<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>本周职工餐品</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <tips optionTemplate="teachFoodWeek"></tips>
        <el-table
            ref="multipleTable"
            :data="arryList"
            border
            tooltip-effect="dark"
            style="width: 100%"
            header-cell-class-name="table-text-c"
            cell-class-name="table-text-c">
          <el-table-column
              prop="time"
              label="日期"
              align="center"
              width="150">
            <template slot-scope="scope">
              {{scope.row.day}}/{{week[scope.row.week]}}
            </template>
          </el-table-column>
          <el-table-column
              prop="food"
              label="菜品名称">
            <template slot-scope="scope">
              <div style="display: flex;">
                <el-tag
                    :key="tag.id"
                    v-for="(tag,index) in scope.row.list"
                    closable
                    class="mr-5"
                    @close="handleClose(scope.$index, index)">
                  {{ tag.food.name }}
                </el-tag>
                <template v-if="scope.row.newFood.length">
                  <el-tag
                      :key="index"
                      v-for="(tag,index) in scope.row.newFood"
                      closable
                      class="mr-5"
                      @close="handleNewClose(scope.$index, index)">
                    {{ tag }}
                  </el-tag>
                </template>
                <el-input
                    class="input-new-tag"
                    v-if="scope.row.visible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm(scope.$index)"
                    @blur="handleInputConfirm(scope.$index)"
                    style="width:100px"
                >
                </el-input>
                <el-button v-else class="button-new-tag" size="small" @click="showInput(scope.$index)">+ 新增菜品
                </el-button>
                <el-button class="button-new-tag" size="small" @click="showPointFn(scope.$index)">查看当天点评
                </el-button>
                <downexcel type="teachFood" :option="{time: scope.row.day}"></downexcel>
                <el-button class="button-new-tag" type="primary" size="small" @click="saveFoodInput(scope.$index)"
                           v-if="scope.row.newFood.length">保存
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">取消</el-button>
        <!--        <el-button size="small" type="primary" @click="checkEditFn">确定保存</el-button>-->
      </div>
      <todayPoint :visible.sync="pointVisible" :pointTime="pointTime"></todayPoint>
    </div>
  </div>
</template>

<script>
import {teachFoodAdd, teachFoodEdit, teachFoodWeek, teachFoodWeekAdd, teachFoodWeekDelete} from '@/api/admin/food'
import todayPoint from "./todayPoint";
export default {
  props: ['visible', 'foodOption'], // recruitmentType examine 审核
  components: {todayPoint},
  data() {
    return {
      dialogVisible: false,
      arryList: [],
      week: {
        1: '星期一',
        2: '星期二',
        3: '星期三',
        4: '星期四',
        5: '星期五',
        6: '星期六',
        0: '星期日',
      },
      inputVisible: false,
      inputValue: '',
      pointVisible: false,
      pointTime: ''
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.getWeekFood()
      }
    }
  },
  created() {

  },
  methods: {
    getWeekFood() {
      teachFoodWeek().then(res => {
        console.log('周表：', res)
        if (res.list.length) {
          for (let item of res.list) {
            item.newFood = []
            item.visible = false
          }
        }
        this.arryList = JSON.parse(JSON.stringify(res.list));
      })
    },
    showInput(index) {
      let option = this.arryList[index];
      option.visible = true
      this.$set(this.arryList, index, option)
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    showPointFn(index){
      let option = this.arryList[index];
      console.log(option)
      this.pointTime = option.day
      this.pointVisible = true
    },
    saveFoodInput(index) {
      let option = this.arryList[index]
      console.log('option:', option)
      teachFoodWeekAdd({create_time: option.day, name: option.newFood.join(',')}).then(res => {
        console.log('res:', res)
        if (!res.code) {
          this.$message.success('保存成功！')
          this.getWeekFood()
        }
      })
    },
    handleInputConfirm(index) {
      let inputValue = this.inputValue;
      let option = this.arryList[index]
      if (inputValue) {
        option.newFood.push(inputValue)
      }
      this.inputValue = '';
      option.visible = false;
      this.$set(this.arryList, index, option)
    },
    handleClose(line, index) {
      let that = this
      this.$confirm('删除当天餐品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.weekFoodDeletFn(line, index)
      }).catch(() => {
      })
    },
    handleNewClose(line, index) {
      let option = this.arryList[line]
      option.newFood.splice(index, 1)
      this.$set(this.arryList, line, option)
    },
    weekFoodDeletFn(line, index) {
      let option = this.arryList[line]
      console.log('detel:', index)
      teachFoodWeekDelete({id: option.list[index].id}).then(res => {
        if (!res.code) {
          this.$message.success('删除成功！')
          option.list.splice(index, 1)
          this.$set(this.arryList, line, option)
        }
      });
    },
    checkEditFn() {
      if (!this.infoForm.name) {
        this.$message.error('请填写餐品名称');
        return
      }
      if (!this.foodOption) {
        teachFoodAdd({name: this.infoForm.name}).then(res => {
          console.log(res)
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('initPage')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        teachFoodEdit({name: this.infoForm.name, id: this.foodOption.id}).then(res => {
          if (!res.code) {
            this.$message.success(res.msg)
            this.$emit('initPage')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 90%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}
</style>


